{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d62854d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6c6ffb07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run shell commands on this MacOS machine.args {{'commands': {{'title': 'Commands', 'description': 'List of shell commands to run. Deserialized using json.loads', 'anyOf': [{{'type': 'string'}}, {{'type': 'array', 'items': {{'type': 'string'}}}}]}}}}\n"
     ]
    }
   ],
   "source": [
    "from langchain.tools import ShellTool\n",
    "\n",
    "shell_tool = ShellTool()\n",
    "\n",
    "shell_tool.description = shell_tool.description + f\"args {shell_tool.args}\".replace(\n",
    "    \"{\", \"{{\"\n",
    ").replace(\"}\", \"}}\")\n",
    "\n",
    "print(shell_tool.description)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "45c78c0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: To list all the Golang source code files in the current directory and its subdirectories, we can use the `find` command in the terminal. The `find` command allows us to search for files in a directory hierarchy. We can specify the `-name` option to search for files with a specific pattern. In this case, we want to find all files with the `.go` extension, which are Golang source code files.\n",
      "\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"terminal\",\n",
      "  \"action_input\": \"find . -name '*.go'\"\n",
      "}\n",
      "```\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3m./llm_friendly_code/demo/skeleton.go\n",
      "./llm_friendly_code/demo/processor_rev.go\n",
      "./llm_friendly_code/demo/token_gen_uuid.go\n",
      "./llm_friendly_code/demo/grpc_endpoint.go\n",
      "./llm_friendly_code/demo/skeleton_interfaces.go\n",
      "./llm_friendly_code/demo/storage_map.go\n",
      "./llm_friendly_code/demo/skeleton_test.go\n",
      "./llm_friendly_code/endpoint/processing_service.pb.go\n",
      "./llm_friendly_code/endpoint/processing_service_grpc.pb.go\n",
      "./llm_friendly_code/main.go\n",
      "\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe terminal command has successfully listed all the Golang source code files in the current directory and its subdirectories. The files are:\n",
      "\n",
      "1. ./llm_friendly_code/demo/skeleton.go\n",
      "2. ./llm_friendly_code/demo/processor_rev.go\n",
      "3. ./llm_friendly_code/demo/token_gen_uuid.go\n",
      "4. ./llm_friendly_code/demo/grpc_endpoint.go\n",
      "5. ./llm_friendly_code/demo/skeleton_interfaces.go\n",
      "6. ./llm_friendly_code/demo/storage_map.go\n",
      "7. ./llm_friendly_code/demo/skeleton_test.go\n",
      "8. ./llm_friendly_code/endpoint/processing_service.pb.go\n",
      "9. ./llm_friendly_code/endpoint/processing_service_grpc.pb.go\n",
      "10. ./llm_friendly_code/main.go\n",
      "\n",
      "Final Answer: The Golang source code files in the current directory and its subdirectories are: skeleton.go, processor_rev.go, token_gen_uuid.go, grpc_endpoint.go\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The Golang source code files in the current directory and its subdirectories are: skeleton.go, processor_rev.go, token_gen_uuid.go, grpc_endpoint.go'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chat_models import AzureChatOpenAI\n",
    "from langchain.agents import initialize_agent\n",
    "from langchain.agents import AgentType\n",
    "\n",
    "llm = AzureChatOpenAI(deployment_name = deployment, model_name=model, temperature=0, max_tokens=200)\n",
    "\n",
    "shell_tool.description = shell_tool.description + f\"args {shell_tool.args}\".replace(\n",
    "    \"{\", \"{{\"\n",
    ").replace(\"}\", \"}}\")\n",
    "self_ask_with_search = initialize_agent(\n",
    "    [shell_tool], llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
    ")\n",
    "self_ask_with_search.run(\n",
    "    \"list all the golang source code's file name in current directory and its sub directoris.\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b9910a8",
   "metadata": {},
   "source": [
    "# Zero-shot Learning"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92d57ae9",
   "metadata": {},
   "source": [
    "## 意图识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3247be3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "def recognize_intent(input):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment, # engine = \"deployment_name\".\n",
    "        temperature = 0,\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"\"\"\n",
    "              Recognize the intent from the user's input and format output as JSON string. \n",
    "            The output JSON string includes: \"intention\", \"paramters\" \"\"\"},\n",
    "            {\"role\": \"user\", \"content\": input}\n",
    "        ]\n",
    "      )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3d8f7d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"intention\": \"book_flight\", \"parameters\": {\"date\": \"8月19日\", \"departure\": \"北京\", \"destination\": \"上海\"}}\n"
     ]
    }
   ],
   "source": [
    "print(recognize_intent(\"订8月19日北京到上海的飞机\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c44bb89",
   "metadata": {},
   "source": [
    "## 情感分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8308e5c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "def classify_sentiment(input):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment, # engine = \"deployment_name\".\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"\"\"\n",
    "             根据给定文字所表达的情感，将文字分为三类：正面 ,中立， 负面。请将用户给定的文字进行分类，并输出分类。\n",
    "             \"\"\"},   \n",
    "            {\"role\": \"user\", \"content\": input}\n",
    "        ],\n",
    "        temperature = 0.9, \n",
    "        max_tokens = 200\n",
    "      )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e7d934fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'正面'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify_sentiment(\"通过前面的课程我对大模型的认识提高了很多。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2f05ebe0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中立'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify_sentiment(\"今天是2023年8月16日\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "264180ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'负面'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classify_sentiment(\"最近感觉很累。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec586f63",
   "metadata": {},
   "source": [
    "# Few-shot Learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0d1c7894",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "def book_flight(input):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment, # engine = \"deployment_name\".\n",
    "        temperature = 0,\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"\"\"\n",
    "              通过用户描述，提取航班预订信息并以JSON格式输出. \n",
    "              以下是一些示例：\n",
    "              示例1\n",
    "              输入: \"订8月9日上海到北京的航班，上午出发\"\n",
    "              输出:\"{\"date\":\"0809\",\"dispatch_time\":\"6-12\",\"from\":\"shanghai\",\"to\":\"beijing\"}\"\n",
    "              \n",
    "              示例2\n",
    "              输入: \"订8月9日上海到北京的航班，下午出发\"\n",
    "              输出:\"{\"date\":\"0809\",\"dispatch_time\":\"12-18\",\"from\":\"shanghai\",\"to\":\"beijing\"}\"\n",
    "              \n",
    "              示例3\n",
    "              输入: \"订8月9日上海到北京的航班，晚上出发\"\n",
    "              输出:\"{\"date\":\"0809\",\"dispatch_time\":\"18-24\",\"from\":\"shanghai\",\"to\":\"beijing\"}\"\n",
    "              \n",
    "              示例4\n",
    "              输入: \"订8月9日上海到北京的航班\"\n",
    "              输出:\"{\"date\":\"0809\",\"dispatch_time\":\"unknown\",\"from\":\"shanghai\",\"to\":\"beijing\"}\"\n",
    "              \"\"\"},\n",
    "            {\"role\": \"user\", \"content\": input}\n",
    "        ]\n",
    "      )\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "35994fe6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"date\":\"0903\",\"dispatch_time\":\"unknown\",\"from\":\"beijing\",\"to\":\"guangzhou\"}'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book_flight(\"预订9月3日北京到广州的航班\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2b533386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"date\":\"unknown\",\"dispatch_time\":\"unknown\",\"from\":\"beijing\",\"to\":\"guangzhou\"}'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book_flight(\"预订北京到广州的航班\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b5a55119",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"date\":\"0903\",\"dispatch_time\":\"18-24\",\"from\":\"unknown\",\"to\":\"guangzhou\"}'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book_flight(\"预订9月3日夜里出发到广州的航班\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78af2f76",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
